/**
 * Created by python on 18-11-11.
 */
let app = new Vue({
    el: '#app',
    data: {
        host: "http://127.0.0.1:8000",
        content:'',
        user_id:sessionStorage.user_id || localStorage.user_id,
        username:sessionStorage.username || localStorage.username,
        avatar:'img/widget-photo.png',
        token:sessionStorage.token || localStorage.token,
        mobile:'',
        labels:'',





    },
    computed: {
        // 需要通过计算得到的数据，键值对 ，键是名称，值是匿名函数



    },

    mounted:function () {
          // 获取分类
        this.get_category();
        // 判断用户的登录状态
        if (this.user_id && this.token) {
            axios.get(this.host + '/user/information/', {
                    // 向后端传递JWT token的方法
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                })
                .then(response => {
                    // 加载用户数据
                    this.user_id = response.data.id;
                    this.username = response.data.username;
                    this.avatar = response.data.avatar;
                })
                .catch(error => {
                    if (error.response.status==401 || error.response.status==403) {
                        location.href = '/login.html?next=/headline-column.html';
                    }
                });
        } else {
            location.href = '/login.html?next=/headline-column.html';
        }
        this.get_label();

    },

    methods: {
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
         logout: function(){
            sessionStorage.clear();
            localStorage.clear();
            this.flush()

        },
        flush(){
            location.reload()
        },
        onSubmit(){
            return false
        },

        get_label:function(){
                axios.get(this.host+ '/label_submit/',{

                        responseType:'json'
                })
                 .then(response => {


                        this.labels = response.data;

                    })
                    .catch(error => {
                        console.log(error.response.data)
                    })
            },

        select_label: function (value) {
            this.label_id.push(value)
        },

    },



})